using System;
using System.Collections.Generic;
using System.Text;
using PlaceFinder.DAL;
using PlaceFinder.BO;
using PlaceFinder.BL.Interfaces;

namespace PlaceFinder.BL
{
    public class PlaceBL : BLBase<PlaceDAO>, IPlaceBL
    {
        
        public List<Place> GetAll()
        {
            List<Place> retval = DAO.GetAll();
            PlacePixelDAO ppDAO = new PlacePixelDAO();
            SetCoordinatesToPlaces(ppDAO.GetAll(), retval);
            return retval;
        }

        

        public Place FindById(int id)
        {
            return DAO.FindById(id);
        }

        public List<Place> Find(string matchWord)
        {
            return DAO.Find(matchWord);
        }

        public List<Place> GetByCategory(int categoryId)
        {
            return DAO.GetByCategory(categoryId);
        }

        private void SetCoordinatesToPlaces(List<PlacePixel> coordinates, List<Place> places)
        {
            int placeIdTemp = -1;
            foreach (PlacePixel placePixel in coordinates)
            {
                if (placeIdTemp != placePixel.PlaceId)
                {
                    placeIdTemp = placePixel.PlaceId;
                    places[placePixel.PlaceId - 1].Coordinates = new List<PlacePixel>();
                }
                places[placePixel.PlaceId - 1].Coordinates.Add(placePixel);
            }            
        }


        public List<Place> GetByFloor(int floorId)
        {
            List<Place> retval = DAO.GetByFloor(floorId);
            PlacePixelDAO placePixelDAO = new PlacePixelDAO();
            SetCoordinatesToPlaces(placePixelDAO.GetByPlaces(retval),retval);
            return retval;
        }


    }
}
